package main

import (
	"flag"
	"fmt"
	"image"
	"image/png"
	"os"

	"github.com/srwiley/oksvg"
	"github.com/srwiley/rasterx"
)

func main() {
	svgFilePath := flag.String("svg", "", "The path to the SVG file to convert")
	pngFilePath := flag.String("png", "", "The path to save the PNG file")
	flag.Parse()

	if *svgFilePath == "" || *pngFilePath == "" {
		fmt.Println("Please specify both svg and png file paths")
		return
	}

	// Open SVG file
	in, err := os.Open(*svgFilePath)
	if err != nil {
		panic(err)
	}
	defer in.Close()

	// Decode SVG file
	icon, err := oksvg.ReadIconStream(in)
	if err != nil {
		panic(err)
	}

	w, h := int(icon.ViewBox.W), int(icon.ViewBox.H)
	icon.SetTarget(0, 0, float64(w), float64(h))

	// Create an image
	img := image.NewRGBA(image.Rect(0, 0, w, h))
	scanner := rasterx.NewScannerGV(w, h, img, img.Bounds())
	raster := rasterx.NewDasher(w, h, scanner)

	// Draw the SVG onto the image
	icon.Draw(raster, 1.0)

	// Save to PNG
	out, err := os.Create(*pngFilePath)
	if err != nil {
		panic(err)
	}
	defer out.Close()

	if err := png.Encode(out, img); err != nil {
		panic(err)
	}

	fmt.Println("SVG successfully converted to PNG")
}
